package com.mg.admin.controller.admin;

import com.mg.admin.bean.admin.DictVO;
import com.mg.admin.bean.admin.LogVO;
import com.mg.admin.bean.admin.Result;
import com.mg.admin.controller.BaseController;
import com.mg.admin.page.PageBean;
import com.mg.admin.page.PageParam;
import com.mg.admin.service.admin.DictService;
import com.mg.admin.service.admin.LogService;
import com.xiaoleilu.hutool.lang.Dict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static com.mg.admin.controller.BaseController.DELETE;
import static com.mg.admin.controller.BaseController.SAVE;

/**
 * 数据字典控制器
 */

@Controller
@RequestMapping(value = "/admin/dict/")
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;

    /**
     * 数据字典分类
     * @param modelMap
     */
    @GetMapping(LIST)
    public void list(ModelMap modelMap) {
        List<DictVO> list = dictService.findAll();
        modelMap.put("list",list);
    }

    /**
     * 数据字典分类修改
     * @param dictVO
     * @return
     */
    @ResponseBody
    @PostMapping(SAVE)
    public DictVO save(DictVO dictVO){
        if (dictVO.getId() == null){
            return dictService.insertOne(dictVO);
        }else {
            return dictService.updateOne(dictVO);
        }
    }

    /**
     * 分类删除
     * @param id
     */
    @ResponseBody
    @PostMapping(DELETE)
    public void delete(Long id){
        dictService.delete(id);
    }

    /**
     * 数据字典分类明细列表
     * @return
     */
    @ResponseBody
    @PostMapping(PAGE)
    public Result<PageBean> queryForPage(DictVO dictVO) {
        PageParam pageParam = new PageParam(dictVO.getStart(),dictVO.getLength());
        PageBean pageBean = dictService.queryForPage(dictVO,pageParam);
        return new Result<>(pageBean);
    }

    @GetMapping("detailadd")
    public void detailadd(ModelMap modelMap,Long classId){
        modelMap.put("classId",classId);
    }

    @GetMapping("detailedit")
    public void detailedit(ModelMap modelMap,Long id){
        DictVO dictVO = dictService.findDetailById(id);
        modelMap.put("bean",dictVO);
    }

    @ResponseBody
    @GetMapping("detaildelete")
    public void  detaildelete(Long id){
        dictService.detaildelete(id);
    }


    @ResponseBody
    @PostMapping("detailsave")
    public Result detailsave(DictVO dictVO){
        if (dictVO.getId() == null){
            dictService.detailInsertOne(dictVO);
        }else {
            dictService.detailUpdateOne(dictVO);
        }
        return  new Result();

    }



}
